#!/usr/bin/env python3
# -*- coding: utf-8 -*-

import re

def extract_path_data(svg_file):
    """从SVG文件中提取路径数据"""
    with open(svg_file, 'r', encoding='utf-8') as f:
        content = f.read()
    
    # 查找path元素
    path_match = re.search(r'<path d="([^"]+)"', content)
    if path_match:
        return path_match.group(1)
    return None

def parse_path_commands(path_data):
    """解析SVG路径命令"""
    commands = []
    
    # 分割路径命令
    parts = re.findall(r'[MLHVCSQTAZ][^MLHVCSQTAZ]*', path_data, re.IGNORECASE)
    
    for part in parts:
        cmd = part[0]
        coords = part[1:].strip()
        if coords:
            # 解析坐标
            coord_nums = re.findall(r'-?\d+\.?\d*', coords)
            commands.append((cmd, coord_nums))
        else:
            commands.append((cmd, []))
    
    return commands

def compare_paths():
    """比较两个SVG文件的路径"""
    
    # 提取路径数据
    ref_path = extract_path_data('python-svg/_1KA_ARCH_P_R_K717_profile.svg')
    our_path = extract_path_data('python-svg/k717_profile_with_arcs.svg')
    
    if not ref_path or not our_path:
        print("无法提取路径数据")
        return
    
    print("参考文件路径:")
    print(ref_path[:200] + "...")
    print("\n我们的文件路径:")
    print(our_path[:200] + "...")
    
    # 解析路径命令
    ref_commands = parse_path_commands(ref_path)
    our_commands = parse_path_commands(our_path)
    
    print(f"\n参考文件命令数: {len(ref_commands)}")
    print(f"我们的文件命令数: {len(our_commands)}")
    
    # 比较弧线命令
    print("\n=== 弧线命令比较 ===")
    
    ref_arcs = [(i, cmd, coords) for i, (cmd, coords) in enumerate(ref_commands) if cmd == 'A']
    our_arcs = [(i, cmd, coords) for i, (cmd, coords) in enumerate(our_commands) if cmd == 'A']
    
    print(f"参考文件弧线数: {len(ref_arcs)}")
    print(f"我们的文件弧线数: {len(our_arcs)}")
    
    for i, ((ref_idx, ref_cmd, ref_coords), (our_idx, our_cmd, our_coords)) in enumerate(zip(ref_arcs, our_arcs)):
        print(f"\n弧线 {i+1}:")
        print(f"  参考: A {' '.join(ref_coords)}")
        print(f"  我们: A {' '.join(our_coords)}")
        
        if len(ref_coords) >= 7 and len(our_coords) >= 7:
            print(f"  半径差异: {ref_coords[0]} vs {our_coords[0]}")
            print(f"  终点差异: ({ref_coords[5]}, {ref_coords[6]}) vs ({our_coords[5]}, {our_coords[6]})")
    
    # 查找弧线前后的L命令
    print("\n=== 弧线前后的直线命令 ===")
    
    for i, (ref_idx, ref_cmd, ref_coords) in enumerate(ref_arcs):
        print(f"\n弧线 {i+1} 前后的命令 (参考文件):")
        
        # 前一个命令
        if ref_idx > 0:
            prev_cmd, prev_coords = ref_commands[ref_idx - 1]
            print(f"  前: {prev_cmd} {' '.join(prev_coords)}")
        
        # 弧线命令
        print(f"  弧: {ref_cmd} {' '.join(ref_coords)}")
        
        # 后一个命令
        if ref_idx < len(ref_commands) - 1:
            next_cmd, next_coords = ref_commands[ref_idx + 1]
            print(f"  后: {next_cmd} {' '.join(next_coords)}")
    
    for i, (our_idx, our_cmd, our_coords) in enumerate(our_arcs):
        print(f"\n弧线 {i+1} 前后的命令 (我们的文件):")
        
        # 前一个命令
        if our_idx > 0:
            prev_cmd, prev_coords = our_commands[our_idx - 1]
            print(f"  前: {prev_cmd} {' '.join(prev_coords)}")
        
        # 弧线命令
        print(f"  弧: {our_cmd} {' '.join(our_coords)}")
        
        # 后一个命令
        if our_idx < len(our_commands) - 1:
            next_cmd, next_coords = our_commands[our_idx + 1]
            print(f"  后: {next_cmd} {' '.join(next_coords)}")

if __name__ == "__main__":
    compare_paths()
